package org.example.utils;


import jakarta.servlet.http.HttpServletRequest;
import org.example.context.SecurityContextHolder;
import org.example.domain.system.constant.TokenConstants;
import org.example.domain.system.model.LoginUser;

/**
 * 权限获取工具类
 */
public class SecurityUtil {


    public static Long getUserId() {
        return SecurityContextHolder.getUserId();
    }

    public static String getUserName() {
        return SecurityContextHolder.getUserName();
    }

    public static LoginUser getLoginUser() {
        return SecurityContextHolder.getLoginUser();
    }


    /**
     * 根据request获取请求token
     */
    public static String getToken(HttpServletRequest request) {
        // 从header获取token标识
        String token = request.getHeader(TokenConstants.AUTHENTICATION);
        // 如果前端设置了令牌前缀，则裁剪掉前缀
        if (StringUtils.isNotEmpty(token) && token.startsWith(TokenConstants.PREFIX)) {
            token = token.replaceFirst(TokenConstants.PREFIX, StringUtils.EMPTY);
        }
        return token;
    }


}
